<?php
/**
 * GioHang 
 * - Define class GioHang
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
include_once(DOCUMENT_ROOT . "/lib/core/BaseProcess.class.php");
class GioHang extends BaseProcess {
	public function __construct() {
		parent::__construct("giohang");
		$this->primaryKeyName = "id";
	}
	
	public function updateSoLuong($gh_user, $gh_sanpham, $del = 0, $sl=1, $money=0, $flat = true) {
		$attr = array('gh_user' => $gh_user, 'gh_sanpham' => $gh_sanpham, 'del' => $del, 'active' => '0');
		$field = array('id', 'soluong', 'tongtien');
		$data = $this->getOneByAttr($attr, $field);
		if (is_array($data) && !empty($data)) {
			$this->setAttribute('soluong', ($flat) ? ($data['soluong'] + $sl) : ($data['soluong'] - $sl));
			$this->setAttribute('tongtien', ($flat) ? ($data['tongtien'] + $money) : ($data['tongtien'] - $money));
			parent::update(array('id' => $data['id']));
		}
	}
	
	
	/**
	 * checkIssetGioHang
	 */
	public function checkIssetGioHang($gh_user, $gh_sanpham, $del = 0) {
		// Connect database
		$sql = "SELECT count(*) as count_id FROM " . $this->tableName . " WHERE gh_user = '" . $gh_user . "' AND gh_sanpham = '" .$gh_sanpham. "' AND del='" . $del . "' AND active='0'";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		return ($rs && $rs->fields['count_id'] > 0) ? true : false;
	}
	
	/**
	 * getGioHangYour
	 */
	public function getGioHangYour($gh_user, $flag = false, $del = 0, $active = 0) {
		if ($flag) {
			$sql = "SELECT * FROM " . $this->tableName;
		} else {
    		$sql = "SELECT id, tongtien FROM " . $this->tableName;
		}
		$sql .= " WHERE gh_user = '" . $gh_user . "' AND del='" . $del . "' AND active= '" . $active . "'";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
        $data = array();
        if ($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) {
                $data[] = $item;
            }
        }
        return $data;
    }
    
	/**
	 * getThongTinGioHang
	 */
	public function getThongTinGioHang() {
		// Connect database
		$sql = "SELECT id, gh_sanpham, soluong FROM " . $this->tableName . " WHERE del='0' AND active='0' AND insert_at <= '" . date ('Y-m-d H:i:s', strtotime('-24 hours', strtotime(date('Y-m-d H:i:s')))) . "'";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		$data = array();
        if ($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) {
                $data[] = $item;
            }
        }
        return $data;
	}
	
	public function getThongTinSPGioHang($atrr, $field, $order) {
		$sql = "SELECT " . $field;
		$sql .= " FROM sp_category as d, sp_sub_category as c, sanpham as s, giohang as gh";
		$sql .= " WHERE d.id=s.iddm AND c.id=s.iddmc AND s.id=gh.gh_sanpham";
		foreach ($atrr as $key=>$value) {
        	if (is_array($value)) {
        		$value['value'] = "'" . mysql_real_escape_string($value['value']) . "'";
	            $sql .= " AND " . $key . $value['math'] . $value['value'];	
        	} else {
        		if ($value !== null) {
	                $value = "'" . mysql_real_escape_string($value) . "'";
	                $sql .= " AND $key=$value";
	            }else {
	                $sql .= " AND $key IS NULL";
	            }	
        	}
        }
        $sql .= " ORDER BY " . $order . " DESC";
        $rs = $this->execute($sql, ADODB_FETCH_ASSOC);
        $result = array();
        if($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) { 
                $result[] = $item;
            }
        }
        return $result;
	}
}
?>